import config from './config.js'
import store from '../store/index.js'
import { fetchEventSource } from '@microsoft/fetch-event-source';
const streamRequest = ({url,data,gptp,success,fail,closed,needLogin=true}) => {
	const URL = (gptp ? config.proxy1 : config.proxy) + url
	const header = needLogin?{token:store.getters.token}:{}
	let eventSource = fetchEventSource(URL, {
		method: 'POST',
		headers: {
		  "Content-Type": 'application/json',
		  ...header
		},
		body: JSON.stringify(data),
		onmessage(event) {
		  success(event.data)
		},
		onerror(err) {
		   fail(err,'fetch-event-source error')
		},
		onclose() {
			console.log('close')
			closed()
		}
	  })
}
export default streamRequest